﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace popLib.ExtensionClass
{
    public abstract class ApplicationSetting
    {        
        public ApplicationSetting()
        {
            InitDefaultValue();
            Load();
        }

        public void Load()
        {
            Type myType = typeof(ApplicationSetting);
            PropertyInfo[] properties = myType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            foreach (PropertyInfo property in properties)
            {
                var value = property.GetValue(myType, null).ToString();

                switch (property.PropertyType.Name)
                {
                    case "Double":
                        property.SetValue(null, NumberLib.getDouble(FormLib.getAppSettingINI(property.ReflectedType.Name, property.Name, value)), null);
                        break;
                    case "Int32":
                        property.SetValue(null, NumberLib.getInt(FormLib.getAppSettingINI(property.ReflectedType.Name, property.Name, value)), null);
                        break;
                    case "String":
                        property.SetValue(null, FormLib.getAppSettingINI(property.ReflectedType.Name, property.Name, value), null);
                        break;
                    default:
                        FormLib.alert(property.PropertyType.Name);
                        property.SetValue(null, FormLib.getAppSettingINI(property.ReflectedType.Name, property.Name, value), null);
                        break;
                }
            }
        }

        public void Save()
        {
            Type myType = typeof(ApplicationSetting);
            PropertyInfo[] properties = myType.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            foreach (PropertyInfo property in properties)
            {
                var value = property.GetValue(myType, null).ToString();
                FormLib.setAppSettingINI(property.ReflectedType.Name, property.Name, value);
            }
        }

        public abstract void InitDefaultValue();
    }
}
